Skip to content
This repository has been archived by the owner on Sep 1, 2020. It is now read-only.

Latest commit

 

History

History
110 lines (94 loc) · 3.67 KB

16.21.2 - 2.0.5.md

File metadata and controls

110 lines (94 loc) · 3.67 KB

2.0.5

Swoole 2.0正式版发布了。2.0版本最大的更新是增加了对协程(Coroutine)的支持。正式版已同时支持PHP5和PHP7。基于Swoole2.0协程PHP开发者可以已同步的方式编写代码,底层自动进行协程调度,转变为异步IO。解决了传统异步编程嵌套回调的问题。

与Node.js(ES6+)、Python等语言使用yield/generator、async/await的实现方式相比,Swoole协程无需修改代码添加额外的关键词。

与Go语言的goroutine相比,Swoole协程是内置式的,应用层代码无需添加go关键词启动协程,只需要使用封装好的协程客户端即可,使用更简单。另外Swoole协程的IO组件在底层内置了超时机制,不需要使用复杂的select/chan/timer实现客户端超时。

目前Swoole底层内置的协程客户端组件包括:udpclient、tcpclient、httpclient、redisclient、mysqlclient,基本涵盖了开发者常用的几种通信协议。协程组件只能在服务器的onConnect、onRequest、onReceive、onMessage 回调函数中使用。

使用示例

$server = new Swoole\Http\Server('127.0.0.1', 9501);

/*
    触发on request事件时,SWOOLE会开辟一个协程栈,对协程栈进行初始化
 */
$server->on('Request', function ($request, $response) {
    $tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
    /**
        client在调用connect函数后,SWOOLE会将PHP上下文信息保存到当前栈内
        然后将协程挂起,待确认连接成功后,触发epoll事件,然后协程切换
        恢复PHP上下文信息,返回结果,继续执行PHP代码
     */
    if ($tcp_cli->connect('127.0.0.1', 9906) === false) {
        $response->end("connect server failed.");
        return;
    }
    $tcp_cli->send('test for the coro');
    /*
        client在调用recv函数后,SWOOLE会将PHP上下文信息保存到当前栈内
        然后将协程挂起待后端svr回包,触发epoll事件,然后协程切换
        恢复PHP上下文信息,返回结果,继续执行PHP代码
        如果后端在设定的超时时间内,未能回包,返回false
        client的errCode定为110
     */
    $ret = $tcp_cli->recv(100);
    $tcp_cli->close();
    if ($ret) {
        $response->end(" swoole response is ok");
    } else {
        $response->end(" recv failed error : {$tcp_cli->errCode}");
    }
});

$server->start();

UDP客户端

$udp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);

$ret = $udp_cli->connect('127.0.0.1', 9906);
$udp_cli->send('test for the coro');

$ret = $udp_cli->recv(100);
$udp_cli->close();

if ($ret)
{
    $response->end("swoole response is ok");
}
else
{
    $response->end("recv failed error : {$client->errCode}");
}

MySQL客户端

$swoole_mysql = new Swoole\Coroutine\MySQL();
$swoole_mysql->connect([
    'host' => '127.0.0.1',
    'user' => 'user',
    'password' => 'pass',
    'database' => 'test'
]);
$res = $swoole_mysql->query('select sleep(1)');
Redis客户端

$redis = new Swoole\Coroutine\Redis();
$redis->connect('127.0.0.1', 6379);
$val = $redis->get('key');

Http客户端

$cli = new Swoole\Coroutine\Http\Client('127.0.0.1', 80);
$cli->setHeaders([
    'Host' => "localhost",
    "User-Agent" => 'Chrome/49.0.2587.3',
    'Accept' => 'text/html,application/xhtml+xml,application/xml',
    'Accept-Encoding' => 'gzip',
]);
$cli->set([ 'timeout' => 1]);
$cli->get('/index.php');
echo $cli->body;  
$cli->close();

下载地址: